Awkward looping behavior

Here is a method that I wrote to create attribute definitions:
 

//Method for creating attribute definitions
void createAttributeDefinitions(){
 
    //Grab selected project
        Project p = proj
 
        //Iterate through items in current project
        Item item = (Item null)
        string iType = ""
        Module m = null
        for item in p do {
 
                //Determine type of item
                iType = type(item)
                if (iType == "Formal" && name(item) == "TestModule") {    //item is a formal module
 
                        //Open module for reading
                        m = read(fullName(item), false)
 
                        //Open module for editing
                        m = edit(fullName(m), true)
 
                        //Set as current module
                        current = m
 
                        //Ensure all attribute types exist
                        t = 0
                        while(t < numOfTypes){
                                        
                                //Extract property
                                AttrType a = get(typeArray,t,1)
                                AttrType at = find (m, a.name)
 
                                //Determine if attribute exists
                                if (null at){                         //attribute does not exist in current module
 
                                        //Create attribute type
                                        createAttributeType(a)
                                }
                                else{}
                                t++
                        }
 
                        //Iterate through elements in array
                        i = 0
                        while(i < numOfElements){
 
                                //Set attribute definition
                                AttrDef__ a = get(keyArray, i, 1)
 
                                //Create attribute
                                AttrDef ad = create a
 
                                //Modify attribute to include description
                                if(!null ad)
                                        ad = modify(ad, setDescription, "Key Attribute")
                                i++
                        }
                
                        //Save changes
                        save(m)
 
                        //Close module
                        close(m)
                }
                else{}
        }
}

 


Now, this code will fail after successfully creating the attributes for one module and opening another module. I'm assuming DXL is weird about opening/closing/saving of modules within a loop - is this so?

 


meherts - Mon Jun 21 13:36:40 EDT 2010

Re: Awkward looping behavior
meherts - Mon Jun 21 14:31:04 EDT 2010

If I remove these two lines of code ...
 

save(m)
close(m)

 


... it will successfully create attributes for the first module it opens, but upon subsequent ones, everything is correct with the exception of the name ... the names instead appear as encrypted characters ... truly strange.

 

Re: Awkward looping behavior
meherts - Mon Jun 21 15:41:55 EDT 2010

meherts - Mon Jun 21 14:31:04 EDT 2010

If I remove these two lines of code ...
 

save(m)
close(m)

 


... it will successfully create attributes for the first module it opens, but upon subsequent ones, everything is correct with the exception of the name ... the names instead appear as encrypted characters ... truly strange.

 

Apparently you can't even do this with skip lists ... I tried to iterate through a skip list and then make the changes and such, but it failed in a similar manner:
 

//Method for creating attributes
void createAttributes(){
 
    //String for manipulation
        string str = ""
 
        //Iterate through skip list
        Module m = null
        for str in skpLst do{
 
                //Key
                string uID = (string key skpLst)
 
                //Grab item
                Item item = itemFromID(uID)
 
                //Open module for reading
                m = read(fullName(item), false)
 
                //Open module for editing
                m = edit(fullName(m), true)
 
                //Set as current module
                current = m
 
                //Ensure all attribute types exist
                t = 0
                while(t < numOfTypes){
                                        
                        //Extract property
                        AttrType a = get(typeArray,t,1)
                        AttrType at = find (m, a.name)
 
                        //Determine if attribute exists
                        if (null at){                         //attribute does not exist in current module
 
                                //Create attribute type
                                createAttributeType(a)
                        }
                        else{}
                        t++
                }
 
                //Iterate through elements in array
                i = 0
                while(i < numOfElements){
 
                        //Set attribute definition
                        AttrDef__ a = get(keyArray, i, 1)
 
                        //Create attribute
                        AttrDef ad = create a
 
                        //Modify attribute to include description
                        if(!null ad)
                                ad = modify(ad, setDescription, "Key Attribute")
                        i++
                }
 
                //Save changes
                save(m)
        
                //Close module
                close(m)
        }
}
 
//Method for populating skip list
void createSkpLst(){
 
        //Grab selected project
        Project p = proj
 
        //Iterate through items in current project
        Item item = (Item null)
        string iType = ""
        Module m = null
        for item in p do {
 
                //Determine type of item
                iType = type(item)
                if (iType == "Formal" && name(item) == "TModule") {      //item is a formal module
 
                        //Put into skip list
                        put(skpLst,uniqueID(item),uniqueID(item))
                }
                else{}
        }
 
        //Create Attributes
        createAttributes()
}

Re: Awkward looping behavior
meherts - Tue Jun 22 09:17:50 EDT 2010

meherts - Mon Jun 21 15:41:55 EDT 2010

Apparently you can't even do this with skip lists ... I tried to iterate through a skip list and then make the changes and such, but it failed in a similar manner:
 

//Method for creating attributes
void createAttributes(){
 
    //String for manipulation
        string str = ""
 
        //Iterate through skip list
        Module m = null
        for str in skpLst do{
 
                //Key
                string uID = (string key skpLst)
 
                //Grab item
                Item item = itemFromID(uID)
 
                //Open module for reading
                m = read(fullName(item), false)
 
                //Open module for editing
                m = edit(fullName(m), true)
 
                //Set as current module
                current = m
 
                //Ensure all attribute types exist
                t = 0
                while(t < numOfTypes){
                                        
                        //Extract property
                        AttrType a = get(typeArray,t,1)
                        AttrType at = find (m, a.name)
 
                        //Determine if attribute exists
                        if (null at){                         //attribute does not exist in current module
 
                                //Create attribute type
                                createAttributeType(a)
                        }
                        else{}
                        t++
                }
 
                //Iterate through elements in array
                i = 0
                while(i < numOfElements){
 
                        //Set attribute definition
                        AttrDef__ a = get(keyArray, i, 1)
 
                        //Create attribute
                        AttrDef ad = create a
 
                        //Modify attribute to include description
                        if(!null ad)
                                ad = modify(ad, setDescription, "Key Attribute")
                        i++
                }
 
                //Save changes
                save(m)
        
                //Close module
                close(m)
        }
}
 
//Method for populating skip list
void createSkpLst(){
 
        //Grab selected project
        Project p = proj
 
        //Iterate through items in current project
        Item item = (Item null)
        string iType = ""
        Module m = null
        for item in p do {
 
                //Determine type of item
                iType = type(item)
                if (iType == "Formal" && name(item) == "TModule") {      //item is a formal module
 
                        //Put into skip list
                        put(skpLst,uniqueID(item),uniqueID(item))
                }
                else{}
        }
 
        //Create Attributes
        createAttributes()
}

Does anyone know why data within my array (keyArray) would get corrupted? ... the name string ends up becoming nonsensical after the first pass, and I feel that it's related to the 'create' statement, where I actually create an attribute definition ... also, creating attribute types seems to not be any trouble at all. This entire ordeal seems unecessary, and extremely frustrating ... I don't see ANY documentation related to loop corruption ... if one can't loop through a project and make changes to modules in 'one fell swoop' through iteration, then I feel like this language is incapable of basic functionality ... any comments and/or help?

Thanks,
m.

Re: Awkward looping behavior
a_vestlin - Tue Jun 22 10:25:24 EDT 2010

meherts - Tue Jun 22 09:17:50 EDT 2010
Does anyone know why data within my array (keyArray) would get corrupted? ... the name string ends up becoming nonsensical after the first pass, and I feel that it's related to the 'create' statement, where I actually create an attribute definition ... also, creating attribute types seems to not be any trouble at all. This entire ordeal seems unecessary, and extremely frustrating ... I don't see ANY documentation related to loop corruption ... if one can't loop through a project and make changes to modules in 'one fell swoop' through iteration, then I feel like this language is incapable of basic functionality ... any comments and/or help?

Thanks,
m.

I am not sure what you store in the keyArray, but it seems like it is objects of the type AttrDef__. Is it so?
I have experienced problems with reference types like this in DXL. A number of types are not persistent, but
only valid in the current context. For example, history posts are only valid inside the loop.

I would guess it is the same with the AttrDef__ type. Usually I solve this kind of issues by creating my own type
and functions to convert the type to the DXL type when to use it. Something like this:

struct MyAttrDef {};
 
MyAttrDef newMyAttrDef(Some parameters...);
 
AttrDef__ getAttrDef(MyAttrDef);
 
// Create list of attrdefs
MyAttrDef med = newMyAttrDef(parameters)
put(skip, i++, med)
.
.
.
 
// Use whats inside list and create attributes
for med in skip do
{
    AttrDef__ ad__ = getAttrDef(med)
    AttrDef ad = create (ad__)
}


Hope this is to any help.

/Anders

Re: Awkward looping behavior
meherts - Tue Jun 22 14:09:59 EDT 2010

@anders - I appreciate your input ... it certainly helped me conceptualize the problem. Oddly enough, the problem turned out to be quite trivial, although fairly subtle. I won't divulge the details of the resolution in order to keep my pride intact, but all that was needed was switch TWO! lines of code ... the woes of a programmer.

Re: Awkward looping behavior
llandale - Wed Jun 23 16:21:34 EDT 2010

I think you need to include the parts of the script that populate your two main arrays, typeArray and keyArray.

No, looping through a folder, opening each module in turn, dealing with that module, then closing it is routine.

I'm suspecting that the AttrType, AttrDef__ variables that you apparently create in your template module, before you invoke this code, only apply to that module. If so, then you cannot 'find' an AttrDef based on an AttrDef__ created from some other module ..err.. created when some other module was current.

If the above is true then I suppose that the AttrType and AttrDef__ variables stored in the Arrays point to memory that has been de-allocated when the template module closed. That memory has not been overwritten during your first module, but does eventually get overwritten sometime thereafter. Is the template module closed?

Perhaps it fails all the time, but your TestModule already has the attr types and defs already created. trap your creates with:

noError()
AttrDef ad = create(...)
string ErrMess = lastError()
if (null ad or !null ErrMess) then print didn't create, ErrMess

 

 

  • Louie


PS1: Declare your variables; turn auto-declare off. For no other reason, folks who DO indeed have it off cannot run your code; whereas code generated with it off works for all other folks.

PS2: Your while loops would be more undertandable like this:

 

 

 

int numOfTypes = 5
int t
for t in 0:numOfTypes-1 do
{  print t "\n"
}